package com.xhu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhu.common.vo.chance.manage.vo.ChanceManageDistributeVo;
import com.xhu.common.vo.chance.manage.vo.ChanceManageInsertVo;
import com.xhu.exceptions.ChanceManageException;
import com.xhu.mapper.ChanceManageMapper;
import com.xhu.pojo.ChanceManage;
import com.xhu.service.ChanceManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;

@Service
public class ChanceManageServiceImpl extends ServiceImpl<ChanceManageMapper, ChanceManage>
        implements ChanceManageService {

    @Autowired
    ChanceManageMapper chanceManageMapper;

    @Override
    public IPage<ChanceManage> getAllByPage(int current, int size) {

        if (current < 1 || size < 1) {
            throw new ChanceManageException("分页失败，查询失败");
        }

        return this.page(new Page<>(current, size));
    }

    @Override
    public IPage<ChanceManage> getAllByEmpNameToPage(String name, int current, int size) {
        if (current < 1 || size < 1) {
            throw new ChanceManageException("分页失败，查询失败");
        }
        LambdaQueryWrapper<ChanceManage> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ChanceManage::getEntryName, name);

        return chanceManageMapper.selectPage(new Page<>(current, size), wrapper);
    }

    @Override
    public void deleteById(String id) {
        if (!StringUtils.hasText(id)) {
            throw new ChanceManageException("删除失败");
        }

        int flag = chanceManageMapper.deleteById(id);
        if (flag != 1) {
            throw new ChanceManageException("删除失败");
        }
    }

    @Override
    public void updateOneById(ChanceManage entity, HttpServletRequest request) {
        if (entity == null) {
            throw new ChanceManageException("实例为空，修改失败");

        }
        if (entity.getChanceId()==null) {
            throw new ChanceManageException("实例为空，修改失败");
        }

        //防止主动修改
        entity.setUpdateTime(null);

        chanceManageMapper.updateById(entity);

    }

    @Override
    public void insertOne(ChanceManageInsertVo entity) {
        if (entity == null) {
            throw new ChanceManageException("实例为空，添加失败");

        }


        ChanceManage manageEntity = new ChanceManage()
                .setChanceCode(entity.getChanceCode())
                .setChanceName(entity.getChanceName())
                .setChanceType(entity.getChanceType())
                .setContent(entity.getContent())
                .setDes(entity.getDes())
                .setChanceId(null);

        chanceManageMapper.insert(manageEntity);

    }

    @Override
    public ChanceManageDistributeVo getSingleChanceInfo(String id) {
        if (!StringUtils.hasText(id)) {
            throw new ChanceManageException("id为空，查询失败");
        }

        ChanceManage res = this.getById(id);

        return new ChanceManageDistributeVo()
                .setChanceId(res.getChanceId())
                .setChanceName(res.getChanceName())
                .setChanceType(res.getChanceType())
                .setOrigin(res.getOrigin())
                .setEntryName(res.getEntryName())
                .setEntryTime(res.getEntryTime())
                .setOperatorName(res.getOperatorName())
                .setContent(res.getContent())
                .setAssignerName(res.getAssignerName());


    }

    @Override
    public void distribute(ChanceManageDistributeVo voEntity) {

        if (!StringUtils.hasText(voEntity.getAssignerName()) || voEntity.getChanceId() == null) {
            throw new ChanceManageException("更新失败");
        }

        ChanceManage entity = new ChanceManage()
                .setChanceId(voEntity.getChanceId())
                .setAssignerName(voEntity.getAssignerName())
                .setEntryName(voEntity.getEntryName())
                .setEntryTime(voEntity.getEntryTime())
                .setOperatorName(voEntity.getOperatorName())
                .setOrigin(voEntity.getOrigin())
                .setCurrentName(voEntity.getCurrentName());

        LambdaUpdateWrapper<ChanceManage> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(ChanceManage::getAssignerName, voEntity.getAssignerName())
                .eq(ChanceManage::getChanceId, voEntity.getChanceId());
        chanceManageMapper.update(entity, wrapper);

    }

}
